<?php
include_once 'qc_base.php';

class PageMaker
{
	public $project_id;
	public $crudset_id;
	
	private $mm;
	
	protected $ar_replacements;
	
	public $tpl_path;
	//$tpl_path = QC_SYS_BASEDIR . '/templates/default/';
	public $out_path;
	
	public $request_vars;
	public $request_vars_error;
	
	public $tpl_file_name;
	public $out_file_name;
	
	public function __construct($project_id){
		$this->project_id = $project_id;
		$this->mm = new MetaModel($project_id);
		
	
	}
	
	public function SetProject($project_id){
		$this->project_id = $project_id;
		}

	public function ProcessTemplate(){
	// open file
	
	// run string replacements
	
	// write file
	
	
	}
	/**
	* GetRepsets($token,$value)
	* This function will execute functions to create custom replacement, and
	* For Booleans:  
	* This function will expand boolean tokens (identified by starting with is_)
	* into a yes_  and no_ prepended values in the array, when the token begins with 'is_'
	* Others:
	* This function will attempt to run the function set_<token> and the replace text will be 
	* set with the return value
	*/
	protected function GetRepsets($token,$value){
		$ar_sub_replacements = array();
		if(substr($token,0,3) == 'is_'){
			if(empty($value)){
				$ar_repset['token'] = 'yes_' . $token;
				$ar_repset['value'] = '';
				$ar_sub_replacements['yes_' . $token] = $ar_repset;
				$ar_repset['token'] = 'no_' . $token;
				$ar_repset['value'] = '//';
				$ar_sub_replacements['no_' . $token] = $ar_repset;
			}else{
				$ar_repset['token'] = 'no_' . $token;
				$ar_repset['value'] = '';
				$ar_sub_replacements['no_' . $token] = $ar_repset;
				$ar_repset['token'] = 'yes_' . $token ;
				$ar_repset['value'] = '//';
				$ar_sub_replacements['yes_' . $token] = $ar_repset;			
			}
		
		}else{
		
			$ar_repset['token'] =  $token ;
			$makefunction = 'set_' . $token;				
			if(method_exists($this,$makefunction)){	
				$ar_repset['value'] = $this->$makefunction($value);
			}else{
				$ar_repset['value'] = $value;
			}
			$ar_sub_replacements[$token] = $ar_repset;
		}
		return $ar_sub_replacements;
	}

	/**
	* DoReplacement($infile,$outfile,$is_inplace = false)
	* This function execute the replacement on the specified file
	* @param $infile  The input file
	* @param $outfile The output file
	* @param $is_inplace (false) If true, re-use the output file as input if the output file exists
	*/
	public function DoReplacement($infile,$outfile,$is_inplace = false){
		$tag = '%';
		if(!$this->GetRequestVars()){
			echo $this->request_vars_error;
		}
		
		$this->GetReplacements();
		
		if($is_inplace){
			if(file_exists($outfile)){
				$infile = $outfile;

			}
			$str_file = file_get_contents($infile);
			echo $str_file;
			$str_file = $this->ReplaceInPlace($str_file,$tag,'/*','*/','END');
		
		}else{
			$str_file = file_get_contents($infile);
			$str_file = $this->ReplaceFromTemplate($str_file,$tag);
		}

		file_put_contents($outfile,$str_file);
		return $str_file;
		
	}
	/**
	* GetRequestVars()
	* This function will attempt to retrive all values in the $request_vars array
	* It will look for a function called set_<token> (where <token> is the request parameter) 
	* and will set the resulting value to the $ar_replacements value for that token
	*/	
	protected function GetRequestVars(){
		if(is_array($this->request_vars)){
			foreach($this->request_vars AS $var){
				if(isset($_REQUEST[$var])){
					$this->ar_replacements[$var]['token'] = $var;
					$value = $_REQUEST[$var];
					$makefunction = 'set_' . $var;
					if(method_exists($this,$makefunction)){	
						$this->ar_replacements[$var]['value'] = $this->$makefunction($value);
					}else{
						$this->ar_replacements[$var]['value'] = $value;
					}					
				}else{
					$this->request_vars_error = "Missing Expected Request variable $var ";
					return false;
				}
			}
		
		}
		return true;
	}
	
	public function GetReplacements(){
		$ar_replace = array();
		echo 'Project ' . $this->project_id . '  or this ' . $this->crudset_id;
		// Get project replacements
		if(empty($this->project_id) ){
			return 'Error - Project or Crudset not defined';
		}
		
		$q = Doctrine_Query::create()
		->from('Projects p')->where('project_id = ?',$this->project_id);
		$ar_projects = $q->fetchArray();
		
			foreach($ar_projects[0] AS $project_variable=>$project_value){
				$ar_repsets = $this->GetRepsets($project_variable,$project_value);
				$ar_replace = array_merge($ar_replace,$ar_repsets);
				
			}
		// pick up any extension replacements
		// function GetExtReplacements needs to return array of token=>data
		if(method_exists($this,'GetExtReplacements')){	
			$ar_ext_replacements = $this->GetExtReplacements();
			foreach($ar_ext_replacements AS $token=>$value){
				$this->ar_replacements[$token]['token'] = $token;
				$this->ar_replacements[$token]['value'] = $value;
			}
			
		}
			
		
		$this->ar_replacements = $ar_replace;
		return $ar_replace;
	
	
	}
	
	protected function set_project_server($value){
		return 'PROCESSED' . $value;
	}
	
	public function ReplaceInPlace($str_start_file,$tag,$startcmt,$endcmt,$endtag = 'END'){
	
		$startcmt_chars = strlen($startcmt);
		$endcmt_chars = strlen($endcmt);
		$tag_chars = strlen($tag);
		$endtag_chars = strlen($endtag);
		//$str_start_file = file_get_contents($filename);
		//echo 'ALL:' . $str_start_file;
//	echo 'ALL:' . $str_start_file;		
	if(strpos($str_start_file,$startcmt . $tag)){

		while(strpos($str_start_file,$startcmt . $tag)){
			$start_pos = strpos($str_start_file,$startcmt . $tag);
			$str_before = substr($str_start_file,0,$start_pos);
			$str_after = substr($str_start_file,$start_pos);
			$end_pos = strpos($str_after, $endtag ) + $tag_chars + $endtag_chars + $endcmt_chars;
			$str_post = substr($str_after,$end_pos);		
			$opcode_end_pos = strpos($str_after,$tag,$tag_chars + $startcmt_chars);
			$str_opcode = substr($str_after,$tag_chars + $startcmt_chars,$opcode_end_pos - $tag_chars - $startcmt_chars - $tagchars);

			if(!empty($this->ar_replacements[$str_opcode]['value'])){
				$data = $this->ar_replacements[$str_opcode]['value'];
			}else{
				$data = '';
			}
//			echo 'ALL:' . $str_start_file;
			$str_new .= $str_before . $startcmt . $tag . $str_opcode . $tag .  $endcmt . ' '. $data .' '. $startcmt . $endtag . $tag. $endcmt;

			//echo '<br />STR_BEFORE:' . $str_before;
			//echo '<br />START POS:' . $start_pos;
			//echo '<br />STR_AFTER:' . $str_after;
			//echo '<br />STR_POST:' . $str_post;
			//echo '<br/ >END_POS:' . $end_pos;
			//echo '<br />OPCODE_END_POS:' . $opcode_end_pos;
			//echo '<br />STR_OPCODE:' . $str_opcode;
			//echo '<br />STR_NEW:' . $str_new;
			$str_start_file = $str_post;
			}
			$str_new .= $str_post;
		}else{
			$str_new = $str_start_file;
		}
//			echo '<br />STR_OUT:' . $str_new;
			return($str_new);
	}

	
	public function ReplaceFromTemplate($str_file,$tag){
		//$str_file = file_get_contents($tpl_file);
		foreach($this->ar_replacements AS $token=>$ar_repset){
			    $str_list_file = str_replace($tag . $token . $tag, $ar_repset['value'], $str_file);
		}
		return $str_file;
	}
	
}

?>